/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2021-2023. All rights reserved.
 *
 */

import { DefaultTargetConst } from '../../const/common-const.js';
import { InjectUtil } from '../../utils/inject-util.js';
import { isTemplatesAfterSourceCodeMigration } from '../../utils/ohos-test-util.js';
import { TaskNames } from '../common/task-names.js';
import { CompileResource } from '../compile-resource.js';
import { LegacyProcessProfile } from '../legacy-tasks/legacy-process-profile.js';
import { GenerateOhosTestTemplate } from '../ohosTest/generate-ohos-test-template.js';
import { ProcessProfile } from '../process-profile.js';
import { ProcessResourceIncrement } from '../process-resource-increment.js';
import { ProcessResource } from '../process-resource.js';
import { ProcessRouterMap } from '../process-router-map.js';
import { ProcessStartupConfig } from '../process-startup-config.js';
import { TargetTaskService } from '../service/target-task-service.js';
import { TargetTaskCreator, TaskCreatorManager } from '../task-creator.js';
import { UnitTestProcessProfile } from '../unitTest/unit-test-process-profile.js';

export const initResourceTasks = (creatorManager: TaskCreatorManager, service: TargetTaskService) => {
  creatorManager.registry(new ProcessResourceCA(service));
  creatorManager.registry(new CompileResourceCA(service));
  creatorManager.registry(new ProcessResourceIncrementCA(service));
};

export class ProcessResourceCA extends TargetTaskCreator {
  declareTaskDetail = () => TaskNames.Task.PROCESS_RESOURCE;

  declareDepends(): string[] {
    const depends = [this.isFa ? LegacyProcessProfile.name : ProcessProfile.name];
    if (InjectUtil.isUnitTestProcess()) {
      depends.push(UnitTestProcessProfile.name);
    }
    return depends;
  }

  provider = () => new ProcessResource(this.targetService);
}

export class CompileResourceCA extends TargetTaskCreator {
  declareTaskDetail = () => TaskNames.Task.COMPILE_RESOURCE;

  declareDepends(): string[] {
    const depends = [ProcessResource.name, ProcessRouterMap.name, ProcessStartupConfig.name];
    const targetName = this.targetService.getTargetData().getTargetName();
    if (targetName !== DefaultTargetConst.OHOS_TEST_TARGET) {
      return depends;
    }
    const sourceRoots = this.targetService.getModuleService().getModuleModel().getSourceRootByTargetName(targetName);
    if (isTemplatesAfterSourceCodeMigration(sourceRoots)) {
      depends.push(GenerateOhosTestTemplate.name);
    }
    return depends;
  }

  provider = () => new CompileResource(this.targetService);
}

export class ProcessResourceIncrementCA extends TargetTaskCreator {
  declareDepends = () => [ProcessProfile.name];

  declareTaskDetail = () => TaskNames.Task.PREVIEW_PROCESS_RESOURCE;

  provider = () => new ProcessResourceIncrement(this.targetService);
}
